package Test;

import DB.AttendanceDB;
import DB.SessionDB;
import DB.StudentDB;
import Model.Attendance;
import Model.Session;
import Model.Student;
import org.junit.Assert;
import org.junit.Test;

import java.util.ArrayList;

import static junit.framework.Assert.fail;

public class AttendanceDBTest {

    //ok
    @Test
    public void testInsertAttendance() throws Exception {
        AttendanceDB attendanceDB = new AttendanceDB();
        StudentDB studentDB = new StudentDB();
        SessionDB sessionDB = new SessionDB();

        Student student = studentDB.findStudentByID(1);
        Session session = sessionDB.findSession(1);

        Attendance attendance = new Attendance(attendanceDB.attendanceGetMax(), "Comment", true, student, session);

        int rc = attendanceDB.insertAttendance(attendance);

        if (rc == 1) {
            System.out.println("Inserting attendance successful");
        }
        else {
            fail("Failed inserting attendance");
        }
    }
    //ok
    @Test
    public void testFindAttendance() throws Exception {

        AttendanceDB attendanceDB = new AttendanceDB();
        int find = 1;

        Attendance attendance = attendanceDB.findAttendance(find);

        if (attendance != null) {
            System.out.println(attendance.getId());
            System.out.println(attendance.getComment());
            System.out.println(attendance.getIsAttending());
            System.out.println(attendance.getStudentId().getName());
            System.out.println(attendance.getStudentId().getTeamId().getName());
            System.out.println(attendance.getStudentId().getTeamId().getName());
            System.out.println(attendance.getSessionId().getId());
            System.out.println(attendance.getSessionId().getTeacherId().getName());
            System.out.println(attendance.getSessionId().getTeacherId().getInitials());
            System.out.println(attendance.getSessionId().getCourseId().getName());
            System.out.println(attendance.getSessionId().getDate());
        }
        else {
            fail("Failed finding attendance - attendance is null");
        }
    }
    //ok
    @Test
    public void testFindAttendanceByStudentID() throws Exception {
        AttendanceDB attendanceDB = new AttendanceDB();
        ArrayList<Attendance> attendanceList = attendanceDB.findAttendanceByStudentID(1);

        if (attendanceList != null) {
            for (Attendance attendance : attendanceList)  {
                System.out.println(attendance.getId());
                System.out.println(attendance.getComment());
                System.out.println(attendance.getIsAttending());
                System.out.println(attendance.getStudentId().getName());
                System.out.println(attendance.getStudentId().getTeamId().getName());
                System.out.println(attendance.getStudentId().getTeamId().getName());
                System.out.println(attendance.getSessionId().getId());
                System.out.println(attendance.getSessionId().getTeacherId().getName());
                System.out.println(attendance.getSessionId().getTeacherId().getInitials());
                System.out.println(attendance.getSessionId().getCourseId().getName());
                System.out.println(attendance.getSessionId().getDate());
            }
        }
        else {
            Assert.fail("No attendances found");
        }
    }

    //okay
    @Test
    public void testGetAttendanceByStudentAndSessionId() throws Exception {
        AttendanceDB attendanceDB = new AttendanceDB();
        StudentDB studentDB = new StudentDB();
        SessionDB sessionDB = new SessionDB();
        Attendance attendance = attendanceDB.getAttendanceByStudentAndSessionId(studentDB.findStudentByID(1),sessionDB.findSession(1));

        if (attendance != null) {
            System.out.println(attendance.getId());
            System.out.println(attendance.getComment());
            System.out.println(attendance.getIsAttending());
            System.out.println(attendance.getStudentId().getName());
            System.out.println(attendance.getStudentId().getTeamId().getName());
            System.out.println(attendance.getStudentId().getTeamId().getName());
            System.out.println(attendance.getSessionId().getId());
            System.out.println(attendance.getSessionId().getTeacherId().getName());
            System.out.println(attendance.getSessionId().getTeacherId().getInitials());
            System.out.println(attendance.getSessionId().getCourseId().getName());
            System.out.println(attendance.getSessionId().getDate());
        }
        else {
            Assert.fail("No attendances found");
        }
    }

    //OK
    @Test
    public void testGetAllAttendancesBySessionID() throws Exception {
        AttendanceDB attendanceDB = new AttendanceDB();
        SessionDB sessionDB = new SessionDB();
        ArrayList<Attendance> attendanceList = attendanceDB.getAllAttendancesBySessionID(sessionDB.findSession(1));

        if (attendanceList != null) {
            for (Attendance attendance : attendanceList)  {
                System.out.println(attendance.getId());
                System.out.println(attendance.getComment());
                System.out.println(attendance.getIsAttending());
                System.out.println(attendance.getStudentId().getName());
                System.out.println(attendance.getStudentId().getTeamId().getName());
                System.out.println(attendance.getStudentId().getTeamId().getName());
                System.out.println(attendance.getSessionId().getId());
                System.out.println(attendance.getSessionId().getTeacherId().getName());
                System.out.println(attendance.getSessionId().getTeacherId().getInitials());
                System.out.println(attendance.getSessionId().getCourseId().getName());
                System.out.println(attendance.getSessionId().getDate());
            }
        }
        else {
            Assert.fail("No attendances found");
        }
    }
    //okay
    @Test
    public void testGetAllAttendances() throws Exception {
        AttendanceDB attendanceDB = new AttendanceDB();
        ArrayList<Attendance> attendanceList = attendanceDB.getAllAttendances(false);

        if (attendanceList != null) {
            for (Attendance attendance : attendanceList) {
                System.out.println(attendance.getId());
                System.out.println(attendance.getComment());
                System.out.println(attendance.getIsAttending());
                System.out.println(attendance.getStudentId().getName());
                System.out.println(attendance.getStudentId().getTeamId().getName());
                System.out.println(attendance.getStudentId().getTeamId().getName());
                System.out.println(attendance.getSessionId().getId());
                System.out.println(attendance.getSessionId().getTeacherId().getName());
                System.out.println(attendance.getSessionId().getTeacherId().getInitials());
                System.out.println(attendance.getSessionId().getCourseId().getName());
                System.out.println(attendance.getSessionId().getDate());
            }
        }

        else {
            Assert.fail("No attendances found");
        }
    }
    //okay
    @Test
    public void testUpdateAttendance() throws Exception {
        AttendanceDB attendanceDB = new AttendanceDB();
        StudentDB studentDB = new StudentDB();
        SessionDB sessionDB = new SessionDB();

        Student student = studentDB.findStudentByID(1);
        Session session = sessionDB.findSession(1);

        boolean updateValue = false;

        Attendance attendance = new Attendance(1, "Comment", updateValue, student, session);
        attendanceDB.updateAttendance(attendance);

        boolean updateResult = attendanceDB.findAttendance(1).getIsAttending();

        if (updateResult == updateValue) {
            System.out.println("Attendance update successful");
        }
        else {
            fail("Attendance update failed");
        }

    }

}
